package com.xer.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zippear {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		Map<String,List<String>> mapFiles = filesToCompress();
			
			if(!mapFiles.isEmpty()){
				Iterator<String> i = mapFiles.keySet().iterator();
					while(i.hasNext()){
						  String name = i.next();
						  compress(name ,mapFiles.get(name));	
					}	
			}
		
	}
	
	private static Map<String,List<String>> filesToCompress(){
		
		Map<String,List<String>> mapFiles = new HashMap<String,List<String>>();
		String name;
		String path;
		List<String> files;
		
		//primer tanda
		name = "db";
		path = "/home/xer/Escritorio/";
		
		files = new ArrayList<String>();
			files.add("/home/xer/Escritorio/java");
			files.add("/home/xer/Escritorio/pad");
			files.add("/home/xer/Escritorio/comandos");
		
		mapFiles.put(path+generateName(name), files);
		//segunda tanda	
		name = "db1";
		path = "/home/xer/Escritorio/";
		
		files = new ArrayList<String>();
			files.add("/home/xer/Escritorio/java");
			files.add("/home/xer/Escritorio/pad");
			files.add("/home/xer/Escritorio/comandos");
		
		mapFiles.put(path+generateName(name), files);
		
		return mapFiles;
	}
	
	private static String generateName(String name){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		return name +" " + df.format((new Date()));
	}

	private static void compress(String filename, List<String> files) {
		int BUFFER_SIZE = 8192;
		try {
			// Reference to the file we will be adding to the zipfile
			BufferedInputStream origin = null;
			// Reference to our zip file
			FileOutputStream dest = new FileOutputStream(filename);
			// Wrap our destination zipfile with a ZipOutputStream
			ZipOutputStream out = new ZipOutputStream(

			new BufferedOutputStream(dest));
			// Create a byte[] buffer that we will read data

			// from the source
			// files into and then transfer it to the zip file
			byte[] data = new byte[BUFFER_SIZE];
			// Iterate over all of the files in our list
			for (Iterator<String> i = files.iterator(); i.hasNext();) {
				// Get a BufferedInputStream that we can use to read the

				// source file
				String fileNameTemp = (String) i.next();
				System.out.println("Adding: " + fileNameTemp);
				FileInputStream fi = new FileInputStream(fileNameTemp);
				origin = new BufferedInputStream(fi, BUFFER_SIZE);
				// Setup the entry in the zip file
				ZipEntry entry = new ZipEntry(fileNameTemp);
				out.putNextEntry(entry);
				// Read data from the source file and write it out to the zip
				// file
				int count;
				while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
					out.write(data, 0, count);
				}
				// Close the source file
				origin.close();
			}
			// Close the zip file
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
